package com.ms.p2p.service.impl;


import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.MemberWithDrawDao;
import com.ms.p2p.entity.account.Withdraw;
import com.ms.p2p.enums.AccountFLow;
import com.ms.p2p.enums.AccountType;
import com.ms.p2p.provider.MemberAccountProvider;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 提现管理
 */
@Service
public class MemberWithDrawServiceImpl {

	protected static Logger logger = Logger.getLogger(MemberWithDrawServiceImpl.class);
	@Autowired MemberWithDrawDao memberWithDrawDao;
	@Autowired
	MemberAccountServiceImpl memberAccountService;


	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void withDrawApply(Withdraw withdraw, String ip) {
		try {
			insert(withdraw);
			memberAccountService.OperAccount(ip,withdraw.getTotal(),withdraw.getUser_id(),"申请提现冻结资金￥" + withdraw.getTotal().setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(), AccountType.cash_frost.getValue(), AccountFLow.FLOW_FROZEN.getValue());
		} catch (Exception e) {
			logger.error("提现失败", e);
			throw new NoticeException("提现失败！");
		}

	}

	public int insert(Withdraw withdraw) {
		return memberWithDrawDao.insert(withdraw);
	}

	public List<Map<String, Object>> findPager(String status, String userId,
			String userName, String begin_date, String end_date, PageBean page) {
		return memberWithDrawDao.findPager(page.getBegin(), page.getPageSize(),
				status, userId, userName, begin_date, end_date);
	}

	public List<Map<String, Object>> findList(String status, String userId,
			String userName, String begin_date, String end_date) {
		return memberWithDrawDao.findPager(0, null, status, userId, userName,
				begin_date, end_date);
	}


	public Map<String, Object> getEmplyoee(int cashId) {
		return memberWithDrawDao.getEmployeeBycashId(cashId);
	}


	public Withdraw findByid(String cashId) {
		return memberWithDrawDao.get(cashId);
	}


	public int update(Withdraw withdraw) {
		return memberWithDrawDao.update(withdraw);
	}

	public String countCash(int user_id) {
		return memberWithDrawDao.recharegeSucess(user_id);
	}

	public int findCount(String status, String userId, String userName,
			String begin_date, String end_date) {
		return memberWithDrawDao.findCount(status, userId, userName, begin_date,
				end_date);

	}

}
